// pages/pay/pay.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile: '',
    address: '',
    upAvater: false,
    avatar: ''
  },
  judgeAuth: function (authStr) {
    return new Promise((resolve, reject) => {
      wx.getSetting({
        success(res) {
          if (!(authStr in res.authSetting) || res.authSetting[authStr]) {
            // 同意授权且调用对应api后取消操作
            resolve(true)
          } else {
            // 未同意授权时，打开微信授权设定列表重新尝试获得授权
            resolve(false)
          }
        },
        fail(err) {
          reject(err)
        }
      })
    })
  },
  chooseAddress: function chooseAddress() {
    if (this.pay_coundDown_flag) {
      return;
    }
    var that = this;
    // 先判断是否已经授权
    this.judgeAuth('scope.address').then(function (isSuccess) {
      if (isSuccess) {
        wx.chooseAddress({
          success: function success(res) {
            // 賦值
            that.setData({
              address: '' + res.provinceName + res.cityName + res.countyName + res.detailInfo,
              receiver: res.userName,
              mobile: res.telNumber,
              user_address_flag: true
            })
            console.log(that.data.address, 101010)
            //缓存收货信息
            wx.setStorage({
              key: 'receiver',
              data: res.userName
            });
            wx.setStorage({
              key: 'mobile',
              data: res.telNumber
            });
            wx.setStorage({
              key: 'address',
              data: that.data.address
            });
          },
          fail: function fail() {
            // that.$parent.toasttips('选择地址失败')
          }
        });
      } else {
        // 未授权则提醒用户打开授权列表
        wx.showModal({
          title: '小鱼提示',
          content: '请允许我们读取您的通信地址',
          showCancel: false,
          success: function success(res) {
            if (res.confirm) {
              wx.openSetting({
                success: function success(res) {
                  if (res.authSetting['scope.address']) {
                    // 使用 call 调用将 this 指向指回 EvaluateCard 类上的原因是，这里直接调用 that.methods 会导致 this 指向 methods
                    // 使其函数重新执行时，上面的this指向全部变为了methods
                    that.methods.chooseAddress.call(that);
                  } else {
                    that.$parent.toasttips('选择地址需同意授权');
                  }
                  if (res.confirm) {
                    console.log('用户点击确定')
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                },
                fail: function fail() {
                  that.$parent.toasttips('调用授权列表管理失败');
                }
              });
            }
          }
        });
      }
    });
  },
      //下单成功提示框
      payFor(){
        // console.log(this.data.receiver,"人")
       if(!this.data.receiver){
        //  console.log("没有地址")
        wx.showToast({
          title: '请选择收货地址！',
          icon: 'none',
          duration: 2000//持续的时间
        })
       }else{
        wx.showModal({
          title: '提示',
          content: '支付前请摸摸你的口袋！',
          success: function (res) {
            if (res.confirm) {//这里是点击了确定以后
              console.log('用户点击确定')
              var _student = wx.getStorageSync('GoodsCarList1');
              let student = []
              _student.forEach(item=>{
                  console.log(item.statu)
                  if(item.statu < 1){
                    item.statu += 1;
                  }
      
                  student.push(item)
              })
              console.log(student,"student")
              wx.setStorageSync('GoodsCarList1', student);
              let set = wx.getStorageSync('GoodsCarList1')
              console.log(wx.getStorageSync('GoodsCarList1'),"111")
              wx.navigateTo({
                url: '/pages/success/success',
              })
            wx.showToast({
                title: '支付成功',
                icon: 'success',
                duration: 2000//持续的时间
              })
              let history =  wx.getStorageSync('history')
              if(history){
                wx.setStorageSync('history', history.concat(set))
              }else{
                let arr = []
                wx.setStorageSync('history', arr.concat(set))
              }
              wx.setStorageSync('GoodsCarList1', [])
            } else {//这里是点击了取消以后
              console.log('用户点击取消')
              wx.switchTab({
                url: '/pages/car/car',
              })
              
            }
          }
        })

       }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.getStorageSync('receiver') && wx.getStorageSync('mobile') && wx.getStorageSync('address')) {
      this.setData({
        user_address_flag: true,
        receiver: wx.getStorageSync('receiver'),
        mobile: wx.getStorageSync('mobile'),
        address: wx.getStorageSync('address')
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      // 获取缓存数据
      var getCardata = wx.getStorageSync('GoodsCarList1');
      let aaa = []
      getCardata.forEach(item=>{
        if(item.statu == 0){
          aaa.push(item)
        }
      })
      this.setData({
          goodsList: aaa,
      })
      this.getTotal(); //计算总价格
  },

  // 计算总价格
  getTotal(){
      // 计算总价格
      let goodsList = this.data.goodsList;

      if(goodsList.length){
          let data = goodsList.reduce((total,item)=>{
              total = total + item.num * item.goodsPrice;
              return total;
          },0)
  
          
          this.setData({
              totalData:data
          })
      }
      

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})